package mx.logging.targets
{
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	
	import mx.core.mx_internal;
	import mx.logging.ILogger;
	import mx.logging.LogEvent;
	
	use namespace mx_internal;
	public class FileTarget extends LineFormattedTarget
	{
		
		private var logFile:File;
		
		public function FileTarget(file:File)
		{
			super();
			this.logFile = file;
			if(file.exists)
			{
				if(file.size > 1000000){
					file.deleteFile();
				}				
			}
		}
		
		public override function logEvent(event : LogEvent) : void {
			// Take care to NOT include mx.*, as code in std lib issues log calls,
			// which will call this target, which will send a message to server
			// (through rpc operation), which will log to this again, ... until StackOverflowException
			var category : String = ILogger(event.target).category;
			if (category.indexOf("mx.") != 0) {
				super.logEvent(event);
			}
		}
		
		override mx_internal function internalLog(message:String):void
		{
			var fileStream:FileStream = new FileStream();  
			fileStream.open(logFile, FileMode.APPEND);
			fileStream.writeUTFBytes(message + "\r\n");
			fileStream.close();
		}
	}
}